#ifndef THINK_THINK_SESSION_H
#define THINK_THINK_SESSION_H

static char* think_session =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"use think\\exception\\ClassNotFoundException;\n"
	"\n"
	"class Session\n"
	"{\n"
	"    protected static $prefix = \'\';\n"
	"    protected static $init   = null;\n"
	"\n"
	"    /**\n"
	"     * 设置或者获取session作用域（前缀）\n"
	"     * @param string $prefix\n"
	"     * @return string|void\n"
	"     */\n"
	"    public static function prefix($prefix = \'\')\n"
	"    {\n"
	"        empty(self::$init) && self::boot();\n"
	"        if (empty($prefix) && null !== $prefix) {\n"
	"            return self::$prefix;\n"
	"        } else {\n"
	"            self::$prefix = $prefix;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * session初始化\n"
	"     * @param array $config\n"
	"     * @return void\n"
	"     * @throws \\think\\Exception\n"
	"     */\n"
	"    public static function init(array $config = [])\n"
	"    {\n"
	"        if (empty($config)) {\n"
	"            $config = Config::get(\'session\');\n"
	"        }\n"
	"        // 记录初始化信息\n"
	"        App::$debug && Log::record(\'[ SESSION ] INIT \' . var_export($config, true), \'info\');\n"
	"        $isDoStart = false;\n"
	"        if (isset($config[\'use_trans_sid\'])) {\n"
	"            ini_set(\'session.use_trans_sid\', $config[\'use_trans_sid\'] ? 1 : 0);\n"
	"        }\n"
	"\n"
	"        // 启动session\n"
	"        if (!empty($config[\'auto_start\']) && PHP_SESSION_ACTIVE != session_status()) {\n"
	"            ini_set(\'session.auto_start\', 0);\n"
	"            $isDoStart = true;\n"
	"        }\n"
	"\n"
	"        if (isset($config[\'prefix\']) && (\'\' === self::$prefix || null === self::$prefix)) {\n"
	"            self::$prefix = $config[\'prefix\'];\n"
	"        }\n"
	"        if (isset($config[\'var_session_id\']) && isset($_REQUEST[$config[\'var_session_id\']])) {\n"
	"            session_id($_REQUEST[$config[\'var_session_id\']]);\n"
	"        } elseif (isset($config[\'id\']) && !empty($config[\'id\'])) {\n"
	"            session_id($config[\'id\']);\n"
	"        }\n"
	"        if (isset($config[\'name\'])) {\n"
	"            session_name($config[\'name\']);\n"
	"        }\n"
	"        if (isset($config[\'path\'])) {\n"
	"            session_save_path($config[\'path\']);\n"
	"        }\n"
	"        if (isset($config[\'domain\'])) {\n"
	"            ini_set(\'session.cookie_domain\', $config[\'domain\']);\n"
	"        }\n"
	"        if (isset($config[\'expire\'])) {\n"
	"            ini_set(\'session.gc_maxlifetime\', $config[\'expire\']);\n"
	"            ini_set(\'session.cookie_lifetime\', $config[\'expire\']);\n"
	"        }\n"
	"        if (isset($config[\'secure\'])) {\n"
	"            ini_set(\'session.cookie_secure\', $config[\'secure\']);\n"
	"        }\n"
	"        if (isset($config[\'httponly\'])) {\n"
	"            ini_set(\'session.cookie_httponly\', $config[\'httponly\']);\n"
	"        }\n"
	"        if (isset($config[\'use_cookies\'])) {\n"
	"            ini_set(\'session.use_cookies\', $config[\'use_cookies\'] ? 1 : 0);\n"
	"        }\n"
	"        if (isset($config[\'cache_limiter\'])) {\n"
	"            session_cache_limiter($config[\'cache_limiter\']);\n"
	"        }\n"
	"        if (isset($config[\'cache_expire\'])) {\n"
	"            session_cache_expire($config[\'cache_expire\']);\n"
	"        }\n"
	"        if (!empty($config[\'type\'])) {\n"
	"            // 读取session驱动\n"
	"            $class = false !== strpos($config[\'type\'], \'\\\\\') ? $config[\'type\'] : \'\\\\think\\\\session\\\\driver\\\\\' . ucwords($config[\'type\']);\n"
	"\n"
	"            // 检查驱动类\n"
	"            if (!class_exists($class) || !session_set_save_handler(new $class($config))) {\n"
	"                throw new ClassNotFoundException(\'error session handler:\' . $class, $class);\n"
	"            }\n"
	"        }\n"
	"        if ($isDoStart) {\n"
	"            session_start();\n"
	"            self::$init = true;\n"
	"        } else {\n"
	"            self::$init = false;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * session自动启动或者初始化\n"
	"     * @return void\n"
	"     */\n"
	"    public static function boot()\n"
	"    {\n"
	"        if (is_null(self::$init)) {\n"
	"            self::init();\n"
	"        } elseif (false === self::$init) {\n"
	"            if (PHP_SESSION_ACTIVE != session_status()) {\n"
	"                session_start();\n"
	"            }\n"
	"            self::$init = true;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * session设置\n"
	"     * @param string        $name session名称\n"
	"     * @param mixed         $value session值\n"
	"     * @param string|null   $prefix 作用域（前缀）\n"
	"     * @return void\n"
	"     */\n"
	"    public static function set($name, $value = \'\', $prefix = null)\n"
	"    {\n"
	"        empty(self::$init) && self::boot();\n"
	"\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$prefix;\n"
	"        if (strpos($name, \'.\')) {\n"
	"            // 二维数组赋值\n"
	"            list($name1, $name2) = explode(\'.\', $name);\n"
	"            if ($prefix) {\n"
	"                $_SESSION[$prefix][$name1][$name2] = $value;\n"
	"            } else {\n"
	"                $_SESSION[$name1][$name2] = $value;\n"
	"            }\n"
	"        } elseif ($prefix) {\n"
	"            $_SESSION[$prefix][$name] = $value;\n"
	"        } else {\n"
	"            $_SESSION[$name] = $value;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * session获取\n"
	"     * @param string        $name session名称\n"
	"     * @param string|null   $prefix 作用域（前缀）\n"
	"     * @return mixed\n"
	"     */\n"
	"    public static function get($name = \'\', $prefix = null)\n"
	"    {\n"
	"        empty(self::$init) && self::boot();\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$prefix;\n"
	"        if (\'\' == $name) {\n"
	"            // 获取全部的session\n"
	"            $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION;\n"
	"        } elseif ($prefix) {\n"
	"            // 获取session\n"
	"            if (strpos($name, \'.\')) {\n"
	"                list($name1, $name2) = explode(\'.\', $name);\n"
	"                $value               = isset($_SESSION[$prefix][$name1][$name2]) ? $_SESSION[$prefix][$name1][$name2] : null;\n"
	"            } else {\n"
	"                $value = isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null;\n"
	"            }\n"
	"        } else {\n"
	"            if (strpos($name, \'.\')) {\n"
	"                list($name1, $name2) = explode(\'.\', $name);\n"
	"                $value               = isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null;\n"
	"            } else {\n"
	"                $value = isset($_SESSION[$name]) ? $_SESSION[$name] : null;\n"
	"            }\n"
	"        }\n"
	"        return $value;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * session获取并删除\n"
	"     * @param string        $name session名称\n"
	"     * @param string|null   $prefix 作用域（前缀）\n"
	"     * @return mixed\n"
	"     */\n"
	"    public static function pull($name, $prefix = null)\n"
	"    {\n"
	"        $result = self::get($name, $prefix);\n"
	"        if ($result) {\n"
	"            self::delete($name, $prefix);\n"
	"            return $result;\n"
	"        } else {\n"
	"            return;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * session设置 下一次请求有效\n"
	"     * @param string        $name session名称\n"
	"     * @param mixed         $value session值\n"
	"     * @param string|null   $prefix 作用域（前缀）\n"
	"     * @return void\n"
	"     */\n"
	"    public static function flash($name, $value)\n"
	"    {\n"
	"        self::set($name, $value);\n"
	"        if (!self::has(\'__flash__.__time__\')) {\n"
	"            self::set(\'__flash__.__time__\', $_SERVER[\'REQUEST_TIME_FLOAT\']);\n"
	"        }\n"
	"        self::push(\'__flash__\', $name);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 清空当前请求的session数据\n"
	"     * @return void\n"
	"     */\n"
	"    public static function flush()\n"
	"    {\n"
	"        if (self::$init) {\n"
	"            $item = self::get(\'__flash__\');\n"
	"\n"
	"            if (!empty($item)) {\n"
	"                $time = $item[\'__time__\'];\n"
	"                if ($_SERVER[\'REQUEST_TIME_FLOAT\'] > $time) {\n"
	"                    unset($item[\'__time__\']);\n"
	"                    self::delete($item);\n"
	"                    self::set(\'__flash__\', []);\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 删除session数据\n"
	"     * @param string|array  $name session名称\n"
	"     * @param string|null   $prefix 作用域（前缀）\n"
	"     * @return void\n"
	"     */\n"
	"    public static function delete($name, $prefix = null)\n"
	"    {\n"
	"        empty(self::$init) && self::boot();\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$prefix;\n"
	"        if (is_array($name)) {\n"
	"            foreach ($name as $key) {\n"
	"                self::delete($key, $prefix);\n"
	"            }\n"
	"        } elseif (strpos($name, \'.\')) {\n"
	"            list($name1, $name2) = explode(\'.\', $name);\n"
	"            if ($prefix) {\n"
	"                unset($_SESSION[$prefix][$name1][$name2]);\n"
	"            } else {\n"
	"                unset($_SESSION[$name1][$name2]);\n"
	"            }\n"
	"        } else {\n"
	"            if ($prefix) {\n"
	"                unset($_SESSION[$prefix][$name]);\n"
	"            } else {\n"
	"                unset($_SESSION[$name]);\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 清空session数据\n"
	"     * @param string|null   $prefix 作用域（前缀）\n"
	"     * @return void\n"
	"     */\n"
	"    public static function clear($prefix = null)\n"
	"    {\n"
	"        empty(self::$init) && self::boot();\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$prefix;\n"
	"        if ($prefix) {\n"
	"            unset($_SESSION[$prefix]);\n"
	"        } else {\n"
	"            $_SESSION = [];\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 判断session数据\n"
	"     * @param string        $name session名称\n"
	"     * @param string|null   $prefix\n"
	"     * @return bool\n"
	"     */\n"
	"    public static function has($name, $prefix = null)\n"
	"    {\n"
	"        empty(self::$init) && self::boot();\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$prefix;\n"
	"        if (strpos($name, \'.\')) {\n"
	"            // 支持数组\n"
	"            list($name1, $name2) = explode(\'.\', $name);\n"
	"            return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]);\n"
	"        } else {\n"
	"            return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 添加数据到一个session数组\n"
	"     * @param  string  $key\n"
	"     * @param  mixed   $value\n"
	"     * @return void\n"
	"     */\n"
	"    public static function push($key, $value)\n"
	"    {\n"
	"        $array = self::get($key);\n"
	"        if (is_null($array)) {\n"
	"            $array = [];\n"
	"        }\n"
	"        $array[] = $value;\n"
	"        self::set($key, $array);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 启动session\n"
	"     * @return void\n"
	"     */\n"
	"    public static function start()\n"
	"    {\n"
	"        session_start();\n"
	"        self::$init = true;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 销毁session\n"
	"     * @return void\n"
	"     */\n"
	"    public static function destroy()\n"
	"    {\n"
	"        if (!empty($_SESSION)) {\n"
	"            $_SESSION = [];\n"
	"        }\n"
	"        session_unset();\n"
	"        session_destroy();\n"
	"        self::$init = null;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 重新生成session_id\n"
	"     * @param bool $delete 是否删除关联会话文件\n"
	"     * @return void\n"
	"     */\n"
	"    public static function regenerate($delete = false)\n"
	"    {\n"
	"        session_regenerate_id($delete);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 暂停session\n"
	"     * @return void\n"
	"     */\n"
	"    public static function pause()\n"
	"    {\n"
	"        // 暂停session\n"
	"        session_write_close();\n"
	"        self::$init = false;\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif